import {Component, OnInit} from '@angular/core';
import {ModalController, NavController, NavParams} from '@ionic/angular';
import {HttpService} from '../../services/http.service';
import {UrlAndOPT} from '../../common/url-and-opt.enum';

@Component({
  selector: 'app-loan-type',
  templateUrl: './loan-type.component.html',
  styleUrls: ['./loan-type.component.scss']
})
export class LoanTypeComponent implements OnInit {

  dataList: any[] = [];
  from = '';

  constructor(
    private navCtrl: NavController,
    private modalCtrl: ModalController,
    private navParams: NavParams,
    private service$: HttpService
  ) {
  }

  ngOnInit() {
    this.getDataList();
  }

  getDataList() {
    // 接收from参数
    this.from = this.navParams.data['from'];
    // 获取借款类型
    this.service$.doRequest(UrlAndOPT.loan_types)
      .subscribe(obj => {
        if (obj) {
          this.dataList = obj['obj'];
        }
      });
  }

  async goLoan(data) {
    // 跳转到借款流程图，然后关闭当前modal
    await this.navCtrl.navigateForward(['/auth/loan', {...data, userRole: 2, from: this.from, fromType: true}]);
    this.closeModal();
  }

  closeModal() {
    this.modalCtrl.dismiss().catch(() => {});
  }

}
